package com.yanqu.road.server.manger.activity.orewar;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.crossorewar.CrossOreStationInfo;
import com.yanqu.road.entity.crossorewar.CrossOreWarNotice;
import com.yanqu.road.entity.crossorewar.CrossOreWarRecord;
import com.yanqu.road.entity.crossorewar.CrossUserOrePit;
import com.yanqu.road.entity.enums.activity.eActivityOreWarType;
import com.yanqu.road.entity.orewar.*;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.entity.union.activitygroup.UnionActivityGroup;
import com.yanqu.road.logic.bussiness.player.UserOreWarBussiness;
import com.yanqu.road.logic.helper.UnionActivityGroupHelper;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.orewar.OreWarProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.pb.OreWarPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.executor.GeneralTask;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.string.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


//盐场争霸活动
public class OreWarMgr extends TempMgr {

    public static final int DISPATCH_ORE_NONE = 0;

    public static final int DISPATCH_ORE_PUBLIC = 1;

    public static final int DISPATCH_ORE_SELF = 2;

    private static Logger logger = LogManager.getLogger(OreWarMgr.class.getName());

    private static Map<Long, UserOreData> userOreDataMap;//用户所有活动的信息合计

    private static Map<Integer, OreWar> oreWarMap = new ConcurrentHashMap<>();//活动map

    private static Map<Integer,Map<Integer,CrossOreWar>> crossOreWarMap = new ConcurrentHashMap<>();//跨服活动map

    private static Map<Integer, Object> activityLock;

    private static Map<Integer,Map<Integer, Object>> crossActivityLock;




    public static void sendCrossBlowoutMessage(int activityId,int groupId){
        synchronized (getActivityLock(activityId)){
            CrossOreWar crossOreWar = getCrossOreWar(activityId,groupId);
            if(null != crossOreWar){
                crossOreWar.sendCrossBlowoutMessage();
            }
        }
    }

    //更新坑位的信息
    public static void refreshCrossOreStation(List<Long> userIdList){
        for (ActivityInfo activityInfo : OreWarActivityMgr.getOpenActivityInfoList()) {
            if (userIdList.isEmpty()){
                return;
            }
            int groupId = GameUnionActivityGroupMgr.getGroupId(activityInfo.getActivityId(),userIdList.get(0));

            OreWarProto.CrossUserBaseInfoSyncMsg.Builder syncMsg = OreWarPb.parseCrossUserBaseInfoSyncMsg(activityInfo.getActivityId(),userIdList);
            GamePlayerMgr.sendUnionActivityGroupPacket(Protocol.C_CROSS_OREWAR_USERBASE_SYNC,syncMsg,0,activityInfo.getActivityId(),groupId);
        }
    }

    public static void sendCrossNamingGetMessage(int activityId,long userId,int stationId,String unionUid,int groupId){
        synchronized (getCrossActivityLock(activityId,groupId)){
            CrossOreWar crossOreWar = getCrossOreWar(activityId,groupId);
            if(null != crossOreWar){
                crossOreWar.crossNamingGetMessage(userId,stationId,unionUid);
            }
        }
    }

    public static void sendCrossNamingLoseMessage(int activityId,UserBaseInfo enemyBaseInfo,int stationId,String oldUnionUid,int groupId){
        synchronized (getCrossActivityLock(activityId,groupId)){
            CrossOreWar crossOreWar = getCrossOreWar(activityId,groupId);
            if(null != crossOreWar){
                crossOreWar.crossNamingLoseMessage(enemyBaseInfo,stationId,oldUnionUid);
            }
        }
    }

    //跨服系统冠名发生变化时候发送奖励
    public static void crossNamingRewardMail(int activityId,Map<Long,Map<Integer,String>> userPitRewardMap,int groupId){
        synchronized (getCrossActivityLock(activityId,groupId)){
            CrossOreWar crossOreWar = getCrossOreWar(activityId,groupId);
            if(null != crossOreWar){
                crossOreWar.crossNamingRewardMail(userPitRewardMap);
            }
        }
    }

    //跨服系统井喷发送井喷前奖励
    public static void crossBlowoutRewardMail(int activityId,Map<Long,Map<Integer,String>> userPitRewardMap,int groupId){
        synchronized (getCrossActivityLock(activityId,groupId)){
            CrossOreWar crossOreWar = getCrossOreWar(activityId,groupId);
            if(null != crossOreWar){
                crossOreWar.crossBlowoutRewardMail(userPitRewardMap);
            }
        }
    }

    //跨服系统撤回领取坑位奖励
    public static void crossSystemRecallRewardMail(int activityId,long userId,Property reward,List<CrossUserOrePit> pitList,int groupId){
        synchronized (getCrossActivityLock(activityId,groupId)){
            CrossOreWar crossOreWar = getCrossOreWar(activityId,groupId);
            if(null != crossOreWar){
                crossOreWar.crossSystemRecallRewardMail(userId,reward,pitList);
            }
        }
    }

    //系统撤回时候进行本地的避难所撤回，必须是跨服撤回后发送到本服后才会触发
    public static Property crossSystemRecallRefuge(int activityId,long userId,long recallTime,int groupId){
        Property reward = new Property();
        synchronized (getCrossActivityLock(activityId,groupId)){
            CrossOreWar crossOreWar = getCrossOreWar(activityId,groupId);
            if(null != crossOreWar){
                return crossOreWar.crossSystemRecallRefuge(userId,recallTime);
            }
        }

        return reward;
    }

    public static void crossRecordSync(int activityId,List<CrossOreWarRecord> crossOreWarRecordList,boolean isAllSync,int groupId){
        synchronized (getCrossActivityLock(activityId,groupId)){
            CrossOreWar crossOreWar = getCrossOreWar(activityId,groupId);
            if(null != crossOreWar){
                crossOreWar.crossRecordSync(crossOreWarRecordList,isAllSync);
            }
        }
    }

    public static void crossNoticeSync(int activityId,List<CrossOreWarNotice> crossOreWarNoticeList,boolean isAllSync,int groupId){
        synchronized (getCrossActivityLock(activityId,groupId)){
            CrossOreWar crossOreWar = getCrossOreWar(activityId,groupId);
            if(null != crossOreWar){
                crossOreWar.crossNoticeSync(crossOreWarNoticeList,isAllSync);
            }
        }
    }

    //跨服坑位信息改变同步坑位信息
    public static void crossPitSync(int activityId,List<CrossUserOrePit> crossUserOrePits,boolean isAllSync,int groupId){
        synchronized (getCrossActivityLock(activityId,groupId)){
            CrossOreWar crossOreWar = getCrossOreWar(activityId,groupId);
            if(null != crossOreWar){
//                crossOreWar.crossPitSync(crossUserOrePits,isAllSync);
            }
        }
    }

    public static void crossPitStationSync(int activityId, List<OreWarProto.CrossOreStationTempMsg> crossOreStationTempMsgList, boolean isAllSync,int groupId){
        synchronized (getCrossActivityLock(activityId,groupId)){
            CrossOreWar crossOreWar = getCrossOreWar(activityId,groupId);
            if(null != crossOreWar){
                crossOreWar.crossPitStationSync(crossOreStationTempMsgList,isAllSync);
            }
        }
    }

    public static void crossPatronsAbilityChange(GamePlayer player, int activityId, int pitId, UserPatrons userPatrons){
        int groupId = GameUnionActivityGroupMgr.getGroupId(activityId, player.getUserId());
        synchronized (getCrossActivityLock(activityId,groupId)){
            CrossOreWar crossOreWar = getCrossOreWar(activityId,groupId);
            if(null != crossOreWar){
                crossOreWar.patronsAbilityChange(player,pitId,userPatrons);
            }
        }
    }

    public static void crossPatronsAbilityChange(GamePlayer player, int activityId, Map<Integer, List<UserPatrons>> patronsMap){
        int groupId = GameUnionActivityGroupMgr.getGroupId(activityId, player.getUserId());
        synchronized (getCrossActivityLock(activityId,groupId)){
            CrossOreWar crossOreWar = getCrossOreWar(activityId,groupId);
            if(null != crossOreWar){
                crossOreWar.crossPatronsAbilityChange(player,patronsMap);
            }
        }
    }

    public static List<CrossOreWarRecord> getCrossUserOreWarRecord(int activityId, long userId){
        int groupId = GameUnionActivityGroupMgr.getGroupId(activityId, userId);
        synchronized (getCrossActivityLock(activityId,groupId)){
            CrossOreWar crossOreWar = getCrossOreWar(activityId,groupId);
            if(null != crossOreWar){
                return crossOreWar.getCrossUserOreWarRecord(userId);
            }
        }
        return new ArrayList<>();
    }

    public static OreWarProto.OreWarNoticeListMsg.Builder getCrossUserOreWarNotice(int activity,GamePlayer player){
        OreWarProto.OreWarNoticeListMsg.Builder msg = OreWarProto.OreWarNoticeListMsg.newBuilder();
        int groupId = GameUnionActivityGroupMgr.getGroupId(activity, player.getUserId());
        synchronized (getCrossActivityLock(activity,groupId)){
            CrossOreWar crossOreWar = getCrossOreWar(activity,groupId);
            if(null != crossOreWar){
                msg = crossOreWar.getCrossUserOreWarNotice(player);
            }
        }
        return msg;
    }

    public static Map<Integer, CrossOreStationInfo> getCrossOreStationMap(int activityId,int groupId){
        synchronized (getCrossActivityLock(activityId,groupId)){
            CrossOreWar crossOreWar = getCrossOreWar(activityId,groupId);
            if(null != crossOreWar){
                return crossOreWar.getCrossOreStationMap();
            }
        }
        return new ConcurrentHashMap<>();
    }


    //跨服领取坑位占领奖励
    public static void addCrossGetReward(int activityId,GamePlayer player,Property reward,int groupId){
        synchronized (getCrossActivityLock(activityId,groupId)){
            CrossOreWar crossOreWar = getCrossOreWar(activityId,groupId);
            if(null != crossOreWar){
                crossOreWar.addCrossGetReward(player,reward);
            }
        }
    }

    //跨服撤回发放奖励
    public static void addCrossRecallReward(int activityId,GamePlayer player,int pitId,long rewardValue,int rewardMin,List<Integer> patronIdList,int groupId){
        synchronized (getCrossActivityLock(activityId,groupId)){
            CrossOreWar crossOreWar = getCrossOreWar(activityId,groupId);
            if(null != crossOreWar){
                crossOreWar.addCrossRecallReward(player,pitId,rewardValue,rewardMin,patronIdList);
            }
        }
    }

    //校验跨服战斗是不是可以上发开始打
    public static int checkCrossRobPit(int activityId,GamePlayer player,int pitId,List<Integer> patronIdList,long pitOwnerUserId,int groupId){
        synchronized (getCrossActivityLock(activityId,groupId)){
            CrossOreWar crossOreWar = getCrossOreWar(activityId,groupId);
            if(null != crossOreWar){
                return crossOreWar.checkCrossRobPit(player,pitId,patronIdList,pitOwnerUserId);
            }
        }
        return GameErrorCode.E_ORE_WAR_ACTIVITY_NO_FOUND;
    }

    //从跨服返回的抢占结果如果是胜利的发送奖励给自己,并且记录这些门客的坑位占领情况
    public static void addCrossOreWarReward(int activityId,GamePlayer player,String reward,int pitId,List<Integer> patronIdList,CrossUserOrePit crossUserOrePit,int groupId){
        synchronized (getCrossActivityLock(activityId,groupId)){
            CrossOreWar crossOreWar = getCrossOreWar(activityId,groupId);
            if(null != crossOreWar){
                crossOreWar.addCrossOreWarReward(player,reward,pitId,patronIdList,crossUserOrePit);
            }
        }
    }

    //在跨服的玩家被打了，到达本服发送邮件奖励，并且撤回他的坑位的人
    public static void addCrossOreWarRewardMail(int activityId, long userId, long attackerUserId, UserBaseInfo attackerBaseData,String reward, int pitId, List<Integer> patronIdList,CrossUserOrePit crossUserOrePit,int groupId){
        synchronized (getCrossActivityLock(activityId,groupId)){
            CrossOreWar crossOreWar = getCrossOreWar(activityId,groupId);
            if(null != crossOreWar){
                crossOreWar.addCrossOreWarRewardMail(userId,attackerUserId,attackerBaseData,reward,pitId,patronIdList,crossUserOrePit);
            }
        }
    }

    //通知跨服商会变更了，结算出用户对于的奖励
    public static void sendCrossUnionInfoChange(int type, long serverId, long userId, String unionUid){
        for (ActivityInfo activityInfo : OreWarActivityMgr.getOpenActivityInfoList()) {
            OreWarProto.CrossOreWarUnionChangeMsg.Builder msg = OreWarProto.CrossOreWarUnionChangeMsg.newBuilder();
            msg.setActivityId(activityInfo.getActivityId());
            msg.setType(type);
            msg.setServerId(serverId);
            msg.setUserId(userId);
            msg.setUnionUid(unionUid);
            int groupId = GameUnionActivityGroupMgr.getGroupId(activityInfo.getActivityId(), unionUid);
            GamePlayerMgr.sendUnionActivityGroupPacket(Protocol.C_CROSS_OREWAR_UNION_INFO_SYNC,msg,0,activityInfo.getActivityId(),groupId);
        }

        /*OreWarProto.CrossUserBaseInfoSyncMsg reqMsg = OreWarProto.CrossUserBaseInfoSyncMsg.parseFrom(packet.getBytes());
        serverId = reqMsg.getServerId();
        userId = reqMsg.getUserId();
        userBaseInfo = PlayerBasePb.parseToUserBaseInfo(reqMsg.getPlayerBaseData());*/
    }

    /*↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑跨服↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑*/

    //第一个建筑的坑位
    public static List<UserOrePit> getTopBuildingOrePitList(int activityId){
        synchronized (getActivityLock(activityId)){
            OreWar oreWar = getOreWar(activityId);
            if(null != oreWar){
                return oreWar.getTopBuildingOrePitList();
            }
        }
        return new ArrayList<>();
    }

    //商会删除动作
    public static void sendUnionDismissNamingChangeReward(String unionUid){
        for(OreWar oreWar : oreWarMap.values()){
            oreWar.sendUnionDismissNamingChangeReward(unionUid);
        }
        sendCrossUnionInfoChange(1,GameServer.getInstance().getServerId(),0,unionUid);
    }

    //加入退出商会操作
    public static void sendUnionNamingJoinQuitReward(String unionUid, long userId){
        synchronized (oreWarMap){
            for(OreWar oreWar : oreWarMap.values()){
                oreWar.sendUnionNamingJoinQuitReward(unionUid,userId);
            }
        }
        sendCrossUnionInfoChange(2,GameServer.getInstance().getServerId(),userId,unionUid);
    }

    //更新坑位的信息
    public static void refreshOreStation(){
        for(OreWar oreWar : oreWarMap.values()){
            oreWar.refreshOreStation();
        }
    }

    public static void patronsAbilityChange(GamePlayer player, int activityId, int pitId, UserPatrons userPatrons){
        synchronized (getActivityLock(activityId)){
            OreWar oreWar = getOreWar(activityId);
            if(null != oreWar){
                oreWar.patronsAbilityChange(player,pitId,userPatrons);
            }
        }
    }

    public static List<OreWarRecord> getUserOreWarRecord(int activityId,long userId){
        synchronized (getActivityLock(activityId)){
            OreWar oreWar = getOreWar(activityId);
            if(null != oreWar){
                return oreWar.getUserOreWarRecord(userId);
            }
        }
        return new ArrayList<>();
    }

    public static OreWarProto.OreWarNoticeListMsg.Builder getUserOreWarNotice(int activity,GamePlayer player){
        OreWarProto.OreWarNoticeListMsg.Builder msg = OreWarProto.OreWarNoticeListMsg.newBuilder();
        synchronized (getActivityLock(activity)){
            OreWar oreWar = getOreWar(activity);
            if(null != oreWar){
                msg = oreWar.getUserOreWarNotice(player);
            }
        }
        return msg;
    }

    public static boolean isInOreWarRecord(int activityId,long userId,long seeUserId){
        synchronized (getActivityLock(activityId)){
            OreWar oreWar = getOreWar(activityId);
            if(null != oreWar){
                return oreWar.isInOreWarRecord(userId,seeUserId);
            }
        }
        return false;
    }

    //领取占领奖励
    public static com.yanqu.road.utils.property.Property getPitOccupyReward(int activityId,long userId, UserOrePit userOrePit, long getRewardTime, boolean isUpdateRewardTime){
        synchronized (getActivityLock(activityId)){
            OreWar oreWar = getOreWar(activityId);
            if(null != oreWar){
                return oreWar.getPitOccupyReward(userId,userOrePit,getRewardTime,isUpdateRewardTime);
            }
        }
        return new Property();
    }

    public static boolean isActivityActive(int activityId){
        synchronized (getActivityLock(activityId)){
            ActivityInfo activityInfo = OreWarActivityMgr.getOpenActivityInfo(activityId);
            if(activityInfo != null){
                if(activityInfo.getChildType() == eActivityOreWarType.OreWar.getChildType()){
                    OreWar oreWar = getOreWar(activityId);
                    if(null != oreWar){
                        return oreWar.isActivityActive();
                    }
                }else if(activityInfo.getChildType() == eActivityOreWarType.CrossOreWar.getChildType()){
                    return isActivityActive(activityInfo);
                }
            }

        }
        return false;
    }

    //活动的每日激活时间
    public static boolean isActivityActive(ActivityInfo activityInfo){
        List<Integer> acList = StringUtils.stringToIntegerList(activityInfo.getParam6(),"\\|");//活动开始在当天的分钟|活动结束在当天的分钟
        long startMinute = acList.get(0);
        long endMinute = acList.get(1);
        if(System.currentTimeMillis() < DateHelper.getTodayZeroTimeStamp() + startMinute*60*1000
                || System.currentTimeMillis() > DateHelper.getTodayZeroTimeStamp() + endMinute*60*1000){
            return false;
        }
        return true;
    }

    //撤回坑位
    public static OreWarProto.OrePitWithdrawRespMsg.Builder orePitRecall(GamePlayer player, int activityId, int pitId){
        OreWarProto.OrePitWithdrawRespMsg.Builder msg = OreWarProto.OrePitWithdrawRespMsg.newBuilder();
        synchronized (getActivityLock(activityId)){
            OreWar oreWar = getOreWar(activityId);
            if(null != oreWar){
                msg = oreWar.orePitRecall(player,pitId);
            }
        }
        return msg;
    }

    public static Map<Integer, OreStationInfo> getOreStationMap(int activityId){
        synchronized (getActivityLock(activityId)){
            OreWar oreWar = getOreWar(activityId);
            if(null != oreWar){
                return oreWar.getOreStationMap();
            }
        }
        return new ConcurrentHashMap<>();
    }

    public static Map<Integer, OrePitInfo> getOrePitInfoMap(int activityId){
        synchronized (getActivityLock(activityId)){
            OreWar oreWar = getOreWar(activityId);
            if(null != oreWar){
                return oreWar.getOrePitInfoMap();
            }
        }
        return new ConcurrentHashMap<>();
    }

    public static OreStationInfo getOreStationInfo(int activityId, int oreStationId){
        synchronized (getActivityLock(activityId)){
            OreWar oreWar = getOreWar(activityId);
            if(null != oreWar){
                return oreWar.getOreStationInfo(oreStationId);
            }
        }
        return null;
    }

    //避难所的也都用这个方法获取奖励
    public static BigDecimal getOreOutput(long userId, int activityId, List<UserPatrons> patronsList, int oreAddition, int unionAddition) {
        ActivityInfo activityInfo = OreWarActivityMgr.getOpenActivityInfo(activityId);
        if (null != activityInfo) {         //首先要有活动
            if (activityInfo.getChildType() == eActivityOreWarType.OreWar.getChildType()) {     //本服的
                synchronized (getActivityLock(activityId)) {
                    OreWar oreWar = getOreWar(activityId);
                    if (null != oreWar) {
                        return oreWar.getOreOutput(userId, patronsList, oreAddition, unionAddition);
                    }
                }
            } else if (activityInfo.getChildType() == eActivityOreWarType.CrossOreWar.getChildType()) {
                int groupId = GameUnionActivityGroupMgr.getGroupId(activityId, userId);
                synchronized (getCrossActivityLock(activityId,groupId)) {
                    CrossOreWar crossOreWar = getCrossOreWar(activityId,groupId);
                    if (null != crossOreWar) {
                        return crossOreWar.getRefugeOreOutput(userId, patronsList, oreAddition);
                    }
                }

            }
        }
        return BigDecimal.ZERO;
    }

    //获取用户单个
    public static UserOrePit getUserOrePit(int activityId, int pitId){
        synchronized (getActivityLock(activityId)){
            OreWar oreWar = getOreWar(activityId);
            if(null != oreWar){
                return oreWar.getUserOrePit(pitId);
            }
        }
        return null;
    }

    //获取用户的坑位信息
    public static List<UserOrePit> getUserOrePitList(int activityId,long userId) {
        List<UserOrePit> pitList = new ArrayList<>();
        synchronized (getActivityLock(activityId)){
            OreWar oreWar = getOreWar(activityId);
            if(oreWar != null){
                pitList = oreWar.getUserOrePitList(userId);
            }
        }
        return pitList;
    }

    public static List<UserOrePit> getUserOrePitList(int activityId, int startPitId, int endPitId) {
        List<UserOrePit> pitList = new ArrayList<>();
        synchronized (getActivityLock(activityId)){
            OreWar oreWar = getOreWar(activityId);
            if(oreWar != null){
                pitList = oreWar.getUserOrePitList(startPitId,endPitId);
            }
        }
        return pitList;
    }

    public static void setUserOreData(UserOreData userOreData){
        if(null != userOreData) {
            userOreDataMap.put(userOreData.getUserId(), userOreData);
        }
    }

    public static UserOreData getUserOreData(long userId){
        return userOreDataMap.get(userId);
    }

    /**
     * 根据活动ID加载活动
     * @param activityId    活动ID
     */
    public static void loadActivity(int activityId) {
        ActivityInfo acInfo = OreWarActivityMgr.getOpenActivityInfo(activityId);
        if(null != acInfo){
            if(acInfo.getChildType() == eActivityOreWarType.OreWar.getChildType()){
                loadServerActivity(activityId);
            }else if(acInfo.getChildType() == eActivityOreWarType.CrossOreWar.getChildType()){
                loadCrossActivity(activityId);
            }
        }
    }

    public static void loadServerActivity(int activityId){
        synchronized (getActivityLock(activityId)) {
            synchronized (oreWarMap) {
                ActivityInfo acInfo = OreWarActivityMgr.getOpenActivityInfo(activityId);
                if(acInfo != null){
                    if (!oreWarMap.containsKey(activityId)) {
                        OreWar oreWar = new OreWar(acInfo);
                        if(oreWar.init()){
                            oreWarMap.put(activityId,oreWar);
                        }else{
                            logger.error("盐场活动"+activityId+"加载失败");
                        }
                    }
                }
            }
        }
    }

    public static void loadCrossActivity(int activityId){
        synchronized (getActivityLock(activityId)) {
            synchronized (crossOreWarMap) {
                ActivityInfo acInfo = OreWarActivityMgr.getOpenActivityInfo(activityId);
                if(acInfo != null){
                    //拿出所有分组
                    Map<Integer, UnionActivityGroup> groupMap = GameUnionActivityGroupMgr.getGroupMap(acInfo.getActivityId());
                    if (groupMap == null){
                        logger.error("跨服盐场活动暂时没有拿到分组数据,等待分组后再一个个加载");
                        return;
                    }
                    if (!crossOreWarMap.containsKey(activityId)) {
                        crossOreWarMap.put(activityId,new ConcurrentHashMap<>());
                    }
                    for (int groupId :groupMap.keySet()){
                        if (!crossOreWarMap.get(activityId).containsKey(groupId)){
                            CrossOreWar crossOreWar = new CrossOreWar(acInfo,groupId);
                            if(crossOreWar.init()){
                                crossOreWarMap.get(activityId).put(groupId,crossOreWar);
                            }else{
                                logger.error("跨服盐场活动"+activityId+"加载失败");
                            }
                        }

                    }
                }
            }
        }
    }

    /**
     * 清除过期数据
     * @param activityId    活动ID
     */
    private static void removeServerData(int activityId) {
        oreWarMap.remove(activityId);
        activityLock.remove(activityId);
        logger.error("remove oreWar activity:{}", activityId);
    }
    private static void removeCrossData(int activityId) {
        crossOreWarMap.remove(activityId);
        activityLock.remove(activityId);
        logger.error("remove oreWar activity:{}", activityId);
    }

    public static void removeActivity(){
        Map<Integer,ActivityInfo> activityInfoMap = OreWarActivityMgr.getOpenActivityInfoMap();
        synchronized (oreWarMap) {
            for(OreWar oreWar : oreWarMap.values()){
                if(!activityInfoMap.containsKey(oreWar.getActivityInfo().getActivityId())){
                    removeServerData(oreWar.getActivityInfo().getActivityId());
                }
            }
        }
        synchronized (crossOreWarMap) {
            for (Map<Integer,CrossOreWar> groupMap: crossOreWarMap.values()){
                for(CrossOreWar crossOreWar : groupMap.values()){
                    if(!activityInfoMap.containsKey(crossOreWar.getActivityInfo().getActivityId())){
                        removeCrossData(crossOreWar.getActivityInfo().getActivityId());
                        break;
                    }
                }
            }
        }
    }
    /**
     * 获取活动锁
     * @param activityId    活动ID
     * @return
     */
    private static synchronized Object getActivityLock(int activityId) {
        if (activityLock == null) {
            activityLock = new ConcurrentHashMap<>();
        }
        if (!activityLock.containsKey(activityId)) {
            activityLock.put(activityId, new Object());
        }
        return activityLock.get(activityId);
    }

    /**
     * 获取跨服活动锁
     * @param activityId    活动ID
     * @return
     */
    private static synchronized Object getCrossActivityLock(int activityId,int groupId) {
        if (crossActivityLock == null) {
            crossActivityLock = new ConcurrentHashMap<>();
        }
        if (!crossActivityLock.containsKey(activityId)) {
            crossActivityLock.put(activityId, new ConcurrentHashMap<>());
        }
        if (!crossActivityLock.get(activityId).containsKey(groupId)){
            crossActivityLock.get(activityId).put(groupId,new Object());
        }
        return crossActivityLock.get(activityId).get(groupId);
    }

    private static OreWar getOreWar(int activityId) {
        synchronized (oreWarMap) {
            if (oreWarMap.containsKey(activityId)) {
                return oreWarMap.get(activityId);
            } else {
                ActivityInfo acInfo = OreWarActivityMgr.getOpenActivityInfo(activityId);
                if (acInfo != null && acInfo.getChildType() == eActivityOreWarType.OreWar.getChildType()) {
                    OreWar oreWar = new OreWar(acInfo);
                    if(oreWar.init()){
                        oreWarMap.put(activityId, oreWar);
                    }else{
                        logger.error("盐场活动"+activityId+"加载失败");
                    }
                    return oreWarMap.get(activityId);
                }
            }
            return null;
        }
    }

    private static CrossOreWar getCrossOreWar(int activityId,int groupId) {
        if (groupId == UnionActivityGroupHelper.NO_GROUP_ID){
            return null;
        }
        synchronized (crossOreWarMap) {
            if (!crossOreWarMap.containsKey(activityId)) {
                crossOreWarMap.put(activityId, new ConcurrentHashMap<>());
            }
            if (crossOreWarMap.get(activityId).containsKey(groupId)) {
                return crossOreWarMap.get(activityId).get(groupId);
            } else {
                ActivityInfo acInfo = OreWarActivityMgr.getOpenActivityInfo(activityId);
                if (acInfo != null && acInfo.getChildType() == eActivityOreWarType.CrossOreWar.getChildType()) {
                    UnionActivityGroup groupData = GameUnionActivityGroupMgr.getGroupData(acInfo.getActivityId(), groupId);
                    if (groupData == null){
                        //如果分组数据为空或者不包含本服,则不加载
                        logger.error("跨服盐场活动"+activityId+"分组数据为空,不加载");
                        return null;
                    }
                    if (!groupData.getServerSet().contains(GameServer.getInstance().getServerId())){
                        //可能是广播过来的 不报错
                        return null;
                    }

                    CrossOreWar crossOreWar = new CrossOreWar(acInfo,groupId);
                     if(crossOreWar.init()){
                        crossOreWarMap.get(activityId).put(crossOreWar.getGroupId(),crossOreWar);
                    }else{
                        logger.error("跨服盐场活动"+activityId+"加载失败");
                    }
                    return crossOreWarMap.get(activityId).get(groupId);
                }
            }
            return null;
        }
    }

    //把所有请求任务添加进去
    public static void addTask(int activityId,GeneralTask task) {
        synchronized (getActivityLock(activityId)){
            OreWar oreWar = getOreWar(activityId);
            if(oreWar != null){
                oreWar.addTask(task);
            }
        }
    }
    public static void addCrossTask(int activityId,GeneralTask task,int groupId) {
        synchronized (getCrossActivityLock(activityId,groupId)){
            CrossOreWar crossOreWar = getCrossOreWar(activityId,groupId);
            if(crossOreWar != null){
                crossOreWar.addTask(task);
            }
        }
    }

    /**
     * 抢夺矿坑
     */
    public static OreWarProto.OrePitRobRespMsg.Builder robPit(GamePlayer player, int activityId, int pitId, long pitOwnerUserId, List<Integer> patronsIdList){
        OreWarProto.OrePitRobRespMsg.Builder msg = OreWarProto.OrePitRobRespMsg.newBuilder();
        msg.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
        synchronized (getActivityLock(activityId)){
            OreWar oreWar = getOreWar(activityId);
            if(oreWar != null){
                msg = oreWar.robPit(player, activityId, pitId, pitOwnerUserId, patronsIdList);
            }
        }
        return msg;
    }

    public static void timerTask(){
        for (OreWar oreWar : oreWarMap.values()) {
            synchronized (getActivityLock(oreWar.getActivityInfo().getActivityId())){
                oreWar.timerTask();
            }
        }

        for (Map<Integer,CrossOreWar> integerCrossOreWarMap: crossOreWarMap.values()) {
            for (CrossOreWar crossOreWar:integerCrossOreWarMap.values()){
                synchronized (getCrossActivityLock(crossOreWar.getActivityInfo().getActivityId(),crossOreWar.getGroupId())){
                    crossOreWar.timerTask();
                }
            }
        }
    }

    public static void reloadCrossPit(){
        for (Map<Integer,CrossOreWar> integerCrossOreWarMap: crossOreWarMap.values()) {
            for (CrossOreWar crossOreWar:integerCrossOreWarMap.values()){
                crossOreWar.reloadCrossPit();
            }
        }
    }

    @Override
    public boolean stop() {
        return false;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean save() {
        for (OreWar oreWar : oreWarMap.values()) {
            oreWar.save();
        }

        for (Map<Integer,CrossOreWar> integerCrossOreWarMap: crossOreWarMap.values()) {
            for (CrossOreWar crossOreWar:integerCrossOreWarMap.values()){
                crossOreWar.save();
            }
        }
        return true;
    }

    @Override
    public boolean reloadData() {
        userOreDataMap = UserOreWarBussiness.getUserOreDataMap();
        return true;
    }
}
